package com.prd.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SysJobDescriptionDTO;
import com.prd.model.SysDepartmentLetterDO;
import com.prd.model.SysJobDescriptionDO;
import com.prd.mapper.SysJobDescriptionMapper;
import com.prd.service.SysJobDescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 岗位说明书 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-04
 */
@Service
public class SysJobDescriptionServiceImpl extends ServiceImpl<SysJobDescriptionMapper, SysJobDescriptionDO> implements SysJobDescriptionService {

    @Autowired
    private SysJobDescriptionMapper sysJobDescriptionMapper;

    /**
     * 添加岗位说明书
     * @param sysJobDescriptionDTO
     * @return
     */
    @Override
    public JsonData add(SysJobDescriptionDTO sysJobDescriptionDTO) {

        SysJobDescriptionDO sysJobDescriptionDO = CommonUtil.dtoTransfer(sysJobDescriptionDTO, SysJobDescriptionDO.class);
        sysJobDescriptionDO.setCreateBy(LoginInterceptor.threadLocal.get().getName());
        sysJobDescriptionDO.setCreatedTime(LocalDateTime.now());
        sysJobDescriptionMapper.insert(sysJobDescriptionDO);

        return JsonData.buildSuccess();
    }


    /**
     * 查看岗位说明书
     * @param id
     * @return
     */
    @Override
    public JsonData detail(Long id) {
        return JsonData.buildSuccess(sysJobDescriptionMapper.selectById(id));
    }

    /**
     * 查看岗位说明书列表
     * @return
     */
    @Override
    public JsonData getAll() {

        return JsonData.buildSuccess(sysJobDescriptionMapper.selectList(new QueryWrapper<>()));
    }

    /**
     * 修改岗位说明书
     * @param describeDTO
     * @return
     */
    @Override
    public JsonData edit(SysJobDescriptionDTO describeDTO) {
        SysJobDescriptionDO descriptionDO = sysJobDescriptionMapper.selectById(describeDTO.getId());
        BeanUtils.copyProperties(describeDTO,descriptionDO);
        int count = sysJobDescriptionMapper.updateById(descriptionDO);
        return count > 0 ? JsonData.buildSuccess() : JsonData.buildError("修改岗位说明书 操作异常");
    }
}
